package com.bdqn.controller;

import com.bdqn.dao.UserMapper;
import com.bdqn.pojo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    @Resource
    UserMapper dao;

    @RequestMapping("/getUser")
    @ResponseBody
    public Object getUser(int index,String id,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        Object [] obj=null;
        String sql ="";
        if (id!=""){
            sql = "users where Permissions in(0,1) and id ="+id;
        }else {
            sql = "users where Permissions in(0,1) ";
        }
        obj = dao.getUser(index,sql);
        List<UserPojo> list = (List<UserPojo>) obj[0];
        int count = ((ArrayList<Integer>)obj[1]).get(0);
        PagePojo page = new PagePojo(index,count,count%10==0?count/10:count/10+1,list);
        return page;
    }


    @RequestMapping("/getfy")
    @ResponseBody
    public List<UserMapper> getfy(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        List<UserMapper> fy = dao.getfy();
        return fy;
    }

    @RequestMapping("/getDel")
    @ResponseBody
    public int getDel(int id,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        int result = dao.getDel(id);
        return result;
    }

    @RequestMapping("/getUpdate")
    @ResponseBody
    public int getUpdate(int id,String phone,String email,String address,String Paddress,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        int result = dao.getUpdate(phone,email,id,address,Paddress);
        return result;
    }
    @RequestMapping("/getUser1")
    @ResponseBody
    public List<UserPojo> getUser1( HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        List<UserPojo> list = dao.getUser1();
        return list;
    }


    @RequestMapping("/getAdd")
    @ResponseBody
    @CrossOrigin
    public int getAdd(@RequestBody UserPojo pojo, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        int result = dao.getAdd(pojo);
        return result;
    }
    @RequestMapping("/getqx")
    @ResponseBody
    public int getqx(String id,String Permissions,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        int result = dao.getqx(Permissions,id);
        return result;
    }




}
